package com.wzm.org.controller;

import com.wzm.basic.util.AjaxResult;
import com.wzm.basic.util.PageList;
import com.wzm.org.domain.Employee;
import com.wzm.org.query.EmployeeQuery;
import com.wzm.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的api" ,description = "员工的crud功能")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 接口：查询所有员工
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有员工")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 接口：查询单个员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个员工")
    Employee findOne(@PathVariable("id") Long id){
        return employeeService.findOne(id);
    }

    @PostMapping
    @ApiOperation(value = "分页查询所有员工")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    /**
     * 接口：通过id删除员工
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除员工")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "新增或者修改员工")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "根据店铺id查询所有店铺人员")
    public List<Employee> findByShopId(@PathVariable("id") Long id){
        return employeeService.findByShopId(id);
    }

}
